package com.china08.yxyapi.api.manage.sch.platform.website.template;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSchoolTemplet;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSchoolTempletRepository;

@CrossOrigin
@RestController
public class SwsTemplateUpdModuleController extends YxyApiBaseController {

    @Autowired
    SwsSchoolTempletRepository SwsSchoolTempletRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/template/module/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {
        List<ParamPojo> list = reqModel.list;

        String schoolId = getProfile(request, response).getSchoolId();

        // 删除
        List<SwsSchoolTemplet> schoolTemplets = SwsSchoolTempletRepository.findBySchoolId(schoolId);
        SwsSchoolTempletRepository.delete(schoolTemplets);

        for (ParamPojo pp : list) {
            Integer orderNum = list.indexOf(pp) + 1;
            SwsSchoolTemplet swsSchoolTemplet = new SwsSchoolTemplet();
            swsSchoolTemplet.setSchoolId(schoolId);
            swsSchoolTemplet.setModuleId(pp.moduleId);
            swsSchoolTemplet.setTemplateId(pp.templateId);
            swsSchoolTemplet.setOrderNum(orderNum);
            SwsSchoolTempletRepository.save(swsSchoolTemplet);
        }
    }

    // 参数对象
    public static class ParamPojo {
        public String templateId;
        public String moduleId;
        public String modouleName;
        public String modouleImage;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public List<ParamPojo> list;
    }

    protected class RespModel extends YxyApiRespModel {
    }
}
